package com.sinopec.android;


import java.util.ArrayList;
import java.util.HashMap;

import com.sinopec.android.bean.ResultData;
import com.sinopec.android.bean.ThreadCallBack;
import com.sinopec.android.bean.UserInfo;
import com.sinopec.android.constant.ConfigAttrs;
import com.sinopec.android.net.ThreadManger;
import com.sinopec.android.util.SharePreferencesUtil;
import com.sinopec.android.util.StringUtil;
import com.sinopec.android.util.ToastUtil;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends BaseActivity implements OnClickListener,ThreadCallBack{

	private String account;
	private String pwd;
	private EditText account_edit;
	private EditText pwd_edit;
	private Button  login_btn;
	private UserInfo user;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_view);
        initView();
    }
    
    void  initView(){
    	account_edit = (EditText) findViewById(R.id.account_edit_text);
    	account = SharePreferencesUtil.getLastLoginAccount(getApplicationContext(), ConfigAttrs.LAST_LOGIN_ACCOUNT);
    	if(StringUtil.isNotEmpty(account)){
    		account_edit.setText(account);
    	}
    	pwd_edit = (EditText) findViewById(R.id.pwd_edit_text);
    	login_btn = (Button)findViewById(R.id.login_btn);
    	login_btn.setOnClickListener(this);
    }

	@Override
	public void onCallbackFromThread(ResultData resultData) {
		// TODO Auto-generated method stub
		if(resultData!=null && resultData.getResultList()!=null){
		}else{
			Toast.makeText(this,R.string.get_fail, 0).show();
			return;
		}
		if(resultData.getResultList().get(0) instanceof String){
			Toast.makeText(this,resultData.getResultList().get(0).toString(), 0).show();
			return ;
		}
		ArrayList arrayList = resultData.getResultList();
		
		//已登录
		if(arrayList.size()>0&&arrayList.get(0) instanceof UserInfo){
			user = (UserInfo) arrayList.get(0);
			if(user!=null){
				String username = user.getUserName();
				//保存登录用户名
				SharePreferencesUtil.saveLoginUserName(getApplicationContext(), username);
				
				//记录登录的账户和密码，方便用户重复输入账号
				SharePreferencesUtil.saveLastLoginAccount(getApplicationContext(), ConfigAttrs.LAST_LOGIN_ACCOUNT, account);
				SharePreferencesUtil.saveLastLoginAccount(getApplicationContext(), ConfigAttrs.LAST_LOGIN_PWD, pwd);
				Intent intent = new Intent();
				intent.setClass(this, HomeActivity.class);
				startActivity(intent);
				finish();
			}
		}
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.login_btn:
			account = account_edit.getText().toString().trim();
			pwd = pwd_edit.getText().toString();
			if(StringUtil.isEmpty(account)){
				ToastUtil.showToast(LoginActivity.this, "账号不能为空");
				return ;
			}else if(StringUtil.isEmpty(pwd)){
				ToastUtil.showToast(LoginActivity.this, "密码不能为空");
				return ;
			}else{
				sendToServer();
			}
			break;

		default:
			break;
		}
	}
	
	private void sendToServer() {
		String reqUrl =ConfigAttrs.BASE_URL+"MobileLogin.ashx";
		HashMap<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("userName", account);
		hashMap.put("passWord", pwd_edit.getText().toString());
		hashMap.put("token", "dbd8e17b 5d3aa4ac 82bc0f7e 226cd06d f5ac3da3 2f53d15a 26ba5051 2cf6d076");
		if(StringUtil.isEmpty(deviceId)){
			deviceId = "0000000000";
		}
		hashMap.put("deviceId", deviceId);

		ThreadManger.exeTask(this, ConfigAttrs.LOGIN_TASK_FLAG, hashMap, reqUrl);
		
	}
}